<?php
/**
 * Created by PhpStorm.
 * User: LENOVO
 * Date: 2018/8/21
 * Time: 23:26
 */

namespace app\api\controller;


use app\api\model\BuyHouseRegister;
use app\api\model\HaveHouseSell;
use app\api\model\HouseToLet;
use app\api\model\RentingRegister;

class WeiTuo
{
  //租房登记
  public function rentingRegister()
  {
    $rentingRegister = new RentingRegister;
    $data = $_POST['formData'];
    $result = $rentingRegister::where('phoneNumber', $data['phoneNumber'])
        ->where('userName', $data['userName'])->find();
    if (!$result) {
      $data['id'] = null;
      $saveResult = $rentingRegister->save($data);
      if ($saveResult) {
        return json([
            'code' => 0,
            'data' => [],
            'msg' => 'ok'
        ]);
      }
    } else {
      $updateResult = $result->save($data);
      if ($updateResult) {
        return json([
            'code' => 2,
            'msg' => '更新成功'
        ]);
      }
    }
  }

  //买房登记
  public function buyHouseRegister()
  {
    $rentingRegister = new BuyHouseRegister;
    $data = $_POST['formData'];
    $result = $rentingRegister::where('phoneNumber', $data['phoneNumber'])
        ->where('userName', $data['userName'])->find();
    if (!$result) {
      $data['id'] = null;
      $saveResult = $rentingRegister->save($data);
      if ($saveResult) {
        return json([
            'code' => 0,
            'data' => [],
            'msg' => 'ok'
        ]);
      }
    } else {
      $updateResult = $result->save($data);
      if ($updateResult) {
        return json([
            'code' => 2,
            'msg' => '更新成功'
        ]);
      }
    }
  }

  //有房出售登记
  public function haveHouseSell()
  {
    $rentingRegister = new HaveHouseSell;
    $data = $_POST['formData'];
    $result = $rentingRegister::where('phoneNumber', $data['phoneNumber'])
        ->where('userName', $data['userName'])->find();
    if (!$result) {
      $data['id'] = null;
      $saveResult = $rentingRegister->save($data);
      if ($saveResult) {
        return json([
            'code' => 0,
            'data' => [],
            'msg' => 'ok'
        ]);
      }
    } else {
      $updateResult = $result->save($data);
      if ($updateResult) {
        return json([
            'code' => 2,
            'msg' => '更新成功'
        ]);
      }
    }
  }

  // 有房出租登记

  public function houseToLet()
  {
    $rentingRegister = new HouseToLet();
    $data = $_POST['formData'];
    $result = $rentingRegister::where('phoneNumber', $data['phoneNumber'])
        ->where('userName', $data['userName'])->find();
    if (!$result) {
      $data['id'] = null;
      $saveResult = $rentingRegister->save($data);
      if ($saveResult) {
        return json([
            'code' => 0,
            'data' => [],
            'msg' => 'ok'
        ]);
      }
    } else {
      $updateResult = $result->save($data);
      if ($updateResult) {
        return json([
            'code' => 2,
            'msg' => '更新成功'
        ]);
      }
    }
  }


}